<?php

namespace App\Http\Controllers\Admin;

use App\Services\WxMp\WxMpListService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * 微信支付应用
 * @package App\Http\Controllers\Admin
 */
class WxMpPayListController extends BaseController
{
    private $mpPayListSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->mpPayListSer = new \App\Services\WxMp\WxMpPayListService();
    }

    /**
     * 应用列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()){
            $pageSize = $request->get('pageSize', 10);
            //状态条件
            $query = \App\Models\WxMpPayList::select('*');
            //关键字
            $keyword = $request->get('keyword', '');
            if ($keyword){
                $query->where('nick_name', 'like', '%'.$keyword.'%');
            }
            return $query->paginate($pageSize);
        }
        return view('admin.wxmppaylist.index', $this->viewData);
    }

    /**
     * 保存应用
     * @param \App\Http\Requests\AdminWxMpListPost $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function save(\App\Http\Requests\AdminWxMpPayListPost $request){
        try {
            $this->mpPayListSer->save($request);
            return $this->showMsg('success', '保存应用成功', 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 删除应用
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        try {
            $this->mpPayListSer->del($request->id ?? 0);
            return $this->showMsg('success', '删除应用成功', [], 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
}
